/**
 * Copyright (C) 2012  The CFuture Android Ad-platform Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lurencun.android.support.ad.i;

import android.app.Dialog;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

/**
 * 
 * @author cfuture.chenyoca [桥下一粒砂] (chenyoca@163.com)
 * @date 2012-2-27
 */
public final class AdTipDialog extends Dialog {

	/**
	 * 弹出窗口背景颜色
	 */
	private static final int BG_COLOR = 0xFF585858;
	
	/**
	 * 提示信息背景颜色
	 */
	private static final int MSG_BG_COLOR = 0xFF222222;
	
	/**
	 * 提示信息文字颜色
	 */
	private static final int MSG_TEXT_COLOR = 0xFFFFFFFF;
	
	/**
	 * 侧边按钮正常状态颜色
	 */
	private static final int SIDE_BTN_COLOR = 0xFFEEEEEE;

	/**
	 * 按钮文字颜色
	 */
	private static final int BTN_TEXT_COLOR = 0xFFF73100;
	
	/**
	 * 中间打开商城按钮颜色
	 */
	private static final int MARKET_BG_COLOR = 0xFFF73100;
	
	/**
	 * 中间打开商城按钮文字颜色
	 */
	private static final int MARKET_TEXT_COLOR = 0xFFFFFFFF;
	
	/**
	 * 三个按钮的文字
	 */
	private static final String UNLOCK_BIT_TEXT = "解锁一次";
	private static final String UNLOCK_MASS_TEXT = "解锁全部";
	private static final String UNLOCK_MARKET_TEXT = "免费获取";
	
	/**
	 * 提示信息
	 */
	private TextView mMessageBox ;
	
	/**
	 * 弹出窗口按钮点击事件回调接口
	 */
	private OnDialogListener mListener;
	
	
	private Context mContext;
	
	
	/**
	 * @param context
	 */
	public AdTipDialog(Context context,OnDialogListener listener) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		mContext = context;
		mListener = listener;
		setContentView(createContentView());
	}

	/**
	 * 设置提示信息。
	 * 在调用 show() 之前，必须调用此方法设置提示信息。
	 * @param msg 提示信息
	 */
	public void setMessage(String msg){
		mMessageBox.setText(msg);
	}

	
	/**
	 * 创建界面，绑定按钮的点击处理
	 * @return
	 */
	private View createContentView() {
		LinearLayout layout = new LinearLayout(mContext);
		layout.setOrientation(LinearLayout.VERTICAL);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		layout.setLayoutParams(params);
		int padding = 10;
		layout.setPadding(padding, padding, padding, padding);
		layout.setBackgroundColor(BG_COLOR);
		mMessageBox = new TextView(mContext);
		mMessageBox.setTextColor(MSG_TEXT_COLOR);
		mMessageBox.setPadding(padding, padding/2, padding, padding);
		mMessageBox.setBackgroundColor(MSG_BG_COLOR);
		layout.addView(mMessageBox);
		
		LinearLayout buttonsLayout = new LinearLayout(mContext);
		buttonsLayout.setOrientation(LinearLayout.HORIZONTAL);
		LayoutParams btnLayoutParam = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		btnLayoutParam.topMargin = 2;
		buttonsLayout.setLayoutParams(btnLayoutParam);
		Button unlockTotal = new Button(mContext);
		Button showMarket = new Button(mContext);
		Button unlockOnce = new Button(mContext);
		
		LayoutParams btnParam = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		btnParam.weight = 1.0f;
		
		unlockTotal.setLayoutParams(btnParam);
		unlockTotal.setText(UNLOCK_MASS_TEXT);
		unlockTotal.setTextColor(BTN_TEXT_COLOR);
		unlockTotal.setBackgroundColor(SIDE_BTN_COLOR);
		unlockTotal.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundColor(BTN_TEXT_COLOR);
					((Button)v).setTextColor(SIDE_BTN_COLOR);
				}else if(event.getAction() == MotionEvent.ACTION_UP){
					v.setBackgroundColor(SIDE_BTN_COLOR);
					((Button)v).setTextColor(BTN_TEXT_COLOR);
					mListener.onUnlocBitData();
				}
				return false;
			}
		});
		
		LayoutParams midParam = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		midParam.leftMargin = 2;
		midParam.rightMargin = 2;
		showMarket.setLayoutParams(midParam);
		showMarket.setText(UNLOCK_MARKET_TEXT);
		showMarket.setBackgroundColor(MARKET_BG_COLOR);
		showMarket.setTextColor(MARKET_TEXT_COLOR);
		showMarket.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundColor(MARKET_TEXT_COLOR);
					((Button)v).setTextColor(MARKET_BG_COLOR);
				}else if(event.getAction() == MotionEvent.ACTION_UP){
					v.setBackgroundColor(MARKET_BG_COLOR);
					((Button)v).setTextColor(MARKET_TEXT_COLOR);
					mListener.onShowOfferWall();
				}
				return false;
			}
		});
		
		unlockOnce.setLayoutParams(btnParam);
		unlockOnce.setText(UNLOCK_BIT_TEXT);
		unlockOnce.setBackgroundColor(SIDE_BTN_COLOR);
		unlockOnce.setTextColor(BTN_TEXT_COLOR);
		unlockOnce.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					v.setBackgroundColor(BTN_TEXT_COLOR);
					((Button)v).setTextColor(SIDE_BTN_COLOR);
				}else if(event.getAction() == MotionEvent.ACTION_UP){
					v.setBackgroundColor(SIDE_BTN_COLOR);
					((Button)v).setTextColor(BTN_TEXT_COLOR);
					mListener.onUnlocBitData();
				}
				return false;
			}
		});
		
		buttonsLayout.addView(unlockTotal);
		buttonsLayout.addView(showMarket);
		buttonsLayout.addView(unlockOnce);
		layout.addView(buttonsLayout);
		return layout;
	}
	
}
